package gorm

// 做个小延申 引入数据库驱动
// import _ "github.com/go-sql-driver/mysql"
// import _ "github.com/jinzhu/gorm/dialects/mysql"
// import _ "github.com/jinzhu/gorm/dialects/postgres"
// import _ "github.com/jinzhu/gorm/dialects/sqlite"
// import _ "github.com/jinzhu/gorm/dialects/mssql"

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"time"
)

type GORMClient = gorm.DB

func NewGORM(info *GORMConfig) (*GORMClient, error) {
	db, err := gorm.Open(info.DBType, info.DSN)
	if err != nil {
		panic(err)
	}
	db.SingularTable(true)
	//db.SetLogger(gorm.Logger{})
	//db.LogMode(info.LogMode)
	db.DB().SetMaxOpenConns(info.MaxOpen)
	db.DB().SetMaxIdleConns(info.MaxIdle)
	db.DB().SetConnMaxLifetime(time.Second * time.Duration(info.ConnMaxLifetime))
	if err = db.DB().Ping(); err != nil {
		return nil, err
	}
	return db, nil
}
